/*
    to inteceptor the http request and validate if the user have logined;
 */

loginInteceptor.$inject = ['$q','$location'];
function loginInteceptor($q,$location){
    var st = 'Inteceptor work log: ';

    var httpLoginInterceptor = {
        request:function(config){
            return config;
        },
        requestError:function(err){
            return $q.reject(err);
        },
        response:function(res) {
            // console.log(st+'response data['+JSON.stringify(res.data)+']');
            console.log(st+'response data');
            // console.log(st+'http response');
            var data = res.data;
            if(data.noLogin){
                $location.path('/');
                return $q.reject(res);
            }
            return res;
        },
        responseError:function(res){
            return $q.reject(res);
        }
    }

    return httpLoginInterceptor;
}

module.exports = loginInteceptor;